package com.example.myretrofit2.activity;

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
OnitemClickListener monitemClickListener;
GestureDetector gestureDetector;//监测手指动作

    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, final OnitemClickListener onitemClickListener){
        this.monitemClickListener = onitemClickListener;
        gestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(),e.getY());
                if(child!=null&&monitemClickListener!=null){
                    monitemClickListener.onLongItemClick(child,recyclerView.getChildAdapterPosition(child));
                }
            }
        });
    }
    @Override
    public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
        View childView = rv.findChildViewUnder(e.getX(),e.getY());
        if(childView!=null&&monitemClickListener!=null){
            monitemClickListener.onItemClick(childView,rv.getChildAdapterPosition(childView));
        }
        gestureDetector.onTouchEvent(e);
        return false;
    }

    @Override
    public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}
